package cn.kgc.ssm.aspects;

import cn.kgc.ssm.annotations.Logs;
import cn.kgc.ssm.entity.LogInfo;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.service.LogInfoService;
import cn.kgc.ssm.utils.IpUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author 课工场
 * @date 2024/3/9
 * @description 日志通知类
 */
@Component
@Aspect
public class LogAspects {

    @Resource
    private LogInfoService logInfoService;

    @Pointcut("@annotation(cn.kgc.ssm.annotations.Logs)")
    public void point() {
    }

    // 环绕通知
    @Around("point()")
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {

        Object proceed = null;
        try {
            long start = System.currentTimeMillis();
            proceed = proceedingJoinPoint.proceed();
            long end = System.currentTimeMillis();
            // 目标方法的耗时
            long timeConsuming = end - start;

            // 获取目标方法的参数
            Object[] args = proceedingJoinPoint.getArgs();
            // 将参数转成字符串 存入数据库
            String params = null;
            if (args.length>0){
               params = StringUtils.arrayToCommaDelimitedString(args);
            }

            // 获取目标方法对象
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            Method method = signature.getMethod();
            Logs annotation = method.getAnnotation(Logs.class);
            // 操作的描述
            String description = annotation.description().getDescription();
            String model = annotation.model();

            // 获取请求对象 request username   ip  url
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            // username
            User user = (User) request.getSession().getAttribute("user");
            String username = user.getUsername();
            String url = request.getServletPath();
            String ip = IpUtil.getIp(request);

            // 封装数据  logInfo
            LogInfo logInfo = new LogInfo();
            logInfo.setUsername(username);
            logInfo.setIp(ip);
            logInfo.setUrl(url);
            logInfo.setModel(model);
            logInfo.setDescription(description);
            logInfo.setParams(params);
            logInfo.setTimeConsuming(timeConsuming);

             logInfoService.add(logInfo);

        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

        return proceed;
    }


}
